//
//  YWSelectDepartmentTableViewController.m
//  maintain
//
//  Created by Kyo on 16/9/5.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWSelectDepartmentTableViewController.h"
#import "UIColor+CustomColors.h"
#import "YWCommonTool.h"
#import "YWMacro.h"

#import "YWContactManager.h"
#import "YWDepartment.h"
#import "YWOrganization.h"

@interface YWSelectDepartmentTableViewController ()
@property (nonatomic, strong) NSMutableArray *selectedArray;
@end

@implementation YWSelectDepartmentTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = NSLocalizedString(@"selectDepartment", nil);//@"选择部门";
    [self initTableView];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"finish", nil) style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonItemEvent:)];
}

- (void)initTableView{
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.separatorColor = [UIColor customLineColor];
    self.tableView.rowHeight = 44.0f;
    self.tableView.tableFooterView = [[UIView alloc]init];
    
    [self.tableView setEditing:YES];
}

- (NSMutableArray *)selectedArray{
    if (!_selectedArray) {
        _selectedArray = [NSMutableArray array];
    }
    return _selectedArray;
}

- (void)setOwnDepartments:(NSArray *)ownDepartments{
    _ownDepartments = ownDepartments;
    [self.selectedArray addObjectsFromArray:ownDepartments];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.departmentArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"selectDepartmentTableVCReuseIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.textLabel.font = kContentFont;
    }
    YWDepartment *department = self.departmentArray[indexPath.row];
    cell.textLabel.text = department.departmentName?:@"";
    //[NSString stringWithFormat:@"%@%@",department.organization.organizationName?:@"",department.departmentName?:@""];
    return cell;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    YWDepartment *department = self.departmentArray[indexPath.row];
    if ([self.selectedArray containsObject:department]) {
        [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    }else{
        [tableView deselectRowAtIndexPath:indexPath animated:NO];
    }
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return NSLocalizedString(@"pleaseSelectDepartment", nil);//@"请选择部门";
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    YWDepartment *department = self.departmentArray[indexPath.row];
    if (![self.selectedArray containsObject:department]) {
        [self.selectedArray addObject:department];
    }
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    YWDepartment *department = self.departmentArray[indexPath.row];
    if ([self.selectedArray containsObject:department]) {
        [self.selectedArray removeObject:department];
    }
}

- (void)rightBarButtonItemEvent:(id)sender{
    if ([self.delegate respondsToSelector:@selector(popSelectedDepartments:)]) {
        [self.delegate popSelectedDepartments:self.selectedArray];
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
